import fetcher from "./Fetcher";
import {Page, Pageable} from "../utils/Page";
import {OffsetDateTime} from "@js-joda/core";

export type Category = keyof { [I in (typeof categories)[number]] }
export const categories = [
    "冰箱",
    "洗衣机",
    "电视",
    "空调",
    "电脑",
    "热水器",
    "家用灶具",
    "吸油烟机",
    "净水器",
    "洗碗机",
    "电饭煲",
    "微波炉",
    "厨房电器",
    "卫浴电器",
    "生活电器",
    "清洁电器",
    "手机",
    "平板电脑",
    "智能手表",
] as const;

export function getHomeApplianceCategories() {
    return categories.filter((_, index) => index <= categories.indexOf("清洁电器"))
}

export function getDigitCategories() {
    return categories.filter((_, index) => index > categories.indexOf("清洁电器"))
}


export interface UpdateOrderRequest {
    name: string;
    idCard?: string;
    phoneNumber?: string;
    paymentFee?: number;
    governmentSubsidies?: number;
    category: Category;  // 假设 XinyuanOrder.Category 映射为 string
    brand: string
    paymentDateTime?: string;
    address?: string;
    energyEfficiencyLevel?: string;
    unionPayOrderNumber?: string;
    invoiceNumber?: string;
    deliveryDateTime?: string;
    barcode?: string;
    serialNumber?: string;
    model?: string;
    imei1?: string;
    imei2?: string;
    idCardFrontPicture?: string;
    idCardBackPicture?: string;
    productImagePictures: string[];
    invoicePicture?: string;
    unionPayInvoicePicture?: string;
    deliveryNotePicture?: string;
}

export function addOrder(request: UpdateOrderRequest) {
    return fetcher.post<OrderResponse>("/xinyuan/orders", {}, request)
}

export interface OrderResponse {
    id: number;
    name: string;
    idCard?: string;
    phoneNumber?: string;
    paymentFee?: number;
    governmentSubsidies?: number;
    category: Category;
    brand: string
    isHomeAppliance: boolean;
    paymentDateTime?: OffsetDateTime;
    address?: string;
    energyEfficiencyLevel?: string;
    unionPayOrderNumber?: string;
    invoiceNumber?: string;
    deliveryDateTime?: OffsetDateTime;
    barcode?: string;
    serialNumber?: string;
    model?: string;
    imei1?: string;
    imei2?: string;
    idCardFrontPicture?: string;
    idCardBackPicture?: string;
    productImagePictures: string[];
    invoicePicture?: string;
    unionPayInvoicePicture?: string;
    deliveryNotePicture?: string;
}

export function updateOrder(id: number, request: UpdateOrderRequest) {
    return fetcher.put<OrderResponse>(`xinyuan/orders/${id}`, {}, request)
}

export interface OrderSearchCriteria {
    isHomeAppliance?: boolean;
    category?: string;  // XinyuanOrder.Category 映射为 string
    startDate?: string; // ISO 8601 格式的日期时间字符串
    endDate?: string;   // ISO 8601 格式的日期时间字符串
    phoneNumber?: string;
    idCard?: string;
    barcode?: string;
    name?: string;
}


export function queryOrders(criteria: OrderSearchCriteria = {}, pageable: Pageable) {
    return fetcher.get<Page<OrderSummary>>("xinyuan/orders", {
        ...criteria,
        ...pageable.toRequestParams()
    })
}

export function getOrderDetail(id: number) {
    return fetcher.get<OrderResponse>(`xinyuan/orders/${id}`)
}

export interface OrderSummary {
    id: number;
    name: string;
    brand: string
    model?: string;
    paymentDateTime?: OffsetDateTime;
    category: string;  // XinyuanOrder.Category 映射为 string
    createdDateTime:OffsetDateTime
}

export const homeApplianceBrands = ["海尔", "格力", "卫泉", "九牧"]
export const digitalBrands = ["OPPO", "Vivo", "华为", "小米"]

export function deleteOrders(id: number) {
    return fetcher.delete(`xinyuan/orders/${id}`)
}